$(function($){
	$("#toAdd").click(function(){
		var url =baseContextPath + "userController/toAdd";
		window.open(url, "", 
		"height=600, width=800, top=50, left=320,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,location=no, resizable=auto");
	});
	
	$("#doAdd").click(function(){
		initAttachInfoBeforeSubmit();
		var url = $("#form").attr("action");
		var data = $("#form").serializeArray();
		$.ajax({
			type:"post",
			url:url,
			dataType:"json",
			data:data,
			async:false,
			error:function(xhr,status,err){
				alert("系统异常");
			},
			success:function(data){
				if(data.result == 'success'){
					alert("添加成功");
					window.opener.reload();
					window.close();
				}else{
					alert(data.msg);
				}
			}
		});
		
	});
	
	function initAttachInfoBeforeSubmit() {
		var resultArray = getItems();
		var json = JSON.stringify(resultArray);
		var jsondata = $.trim(json);
		$('#jsonData').val(jsondata);
	}
	
	
	function getItems() {
		var jQueryItems = $('.addAnotherItem tr');
		var jsonArray = jsonArray || [];
		jQueryItems.each(function() {
			var name = $(this).find("[name=name]").val(); // 银行名称
			var loginName = $(this).find('[name=loginName]').val(); // 帐号
			var pwd = $(this).find('[name=pwd]').val(); // 帐号
			var goodsPrice = $(this).find('[name=goodsPrice]').val(); // 帐号
			if (name && loginName && pwd && goodsPrice) {
				var item = {
						name : name,
						loginName : loginName,
						pwd : pwd,
						goodsPrice : goodsPrice
				};
				jsonArray.push(item);
			};
		});

		return jsonArray;
		
	}
	
	
	$("input[name='toUpdate']").click(function(){
		var id = $(this).attr('lang');
		var url =baseContextPath + "userController/toUpdate?id="+id;
		window.open(url, "", 
		"height=600, width=800, top=50, left=320,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,location=no, resizable=auto");
	});
	
	$("#doAddAnother").click(function(){
		var isAllowed = isAllowedToAdd();
		if(!isAllowed){
			alert("请填写完数据再新加数据!");
			return;
		}
		innerhtml='';
		 innerhtml+= '<tr>';
		 innerhtml+= '<td> <input type="text" name="name"/></td>';
		 innerhtml+= '<td><input type="text" name="loginName"/></td>';
		 innerhtml+= '<td>'+createSelect("hourStart", '01', 3)+'</td>';
		 innerhtml+= '<td><input type="text" name="goodsPrice" /></td>';
		 innerhtml+= '<td><input type="button" name="delete" value="删除"  /></td>';
		 innerhtml +='</tr>';
		 $(".addAnotherItem").append(innerhtml);
	});
	
	function createSelect(name, value, length){
		innerhtml='<select name='+ name + '>';
		for(var i = 0; i < length; i++){
			var val = i + "";
			if(i < 10){
				val = "0" + i;
			}
			innerhtml += '<option value= "'+i+ '" '+(val == value ? "selected" : "") +'>'+val+'</option>';
		}
		innerhtml +='</select>';
		return innerhtml;
	}
	
	function isAllowedToAdd() {
		var isAllowed = false;
		var items = $('.addAnotherItem tr');
		items.each(function() {
			isAllowed = false;
			var name = $(this).find("[name=name]").val(); // 银行名称
			var loginName = $(this).find('[name=loginName]').val(); // 帐号
			var pwd = $(this).find('[name=pwd]').val(); // 帐号
			var goodsPrice = $(this).find('[name=goodsPrice]').val(); // 帐号
			if (name && loginName && pwd && goodsPrice) {
				isAllowed = true;
			}
		});
		return isAllowed;
	}
	
	$(".addAnotherItem").on("click","tr input[name='delete']",function(){
		var tr = $(this).parents('tr:first');
		$(tr).remove();
	});
	
})